#include "SpriteHelper.h"
#include "GameConstants.h"

USING_NS_CC;
SpriteHelper::SpriteHelper(void)
{
}


SpriteHelper::~SpriteHelper(void)
{
}


/*
  init frame animation and cache.
*/
 void SpriteHelper::initSpriteActionAnimation(int startFrameIndex,int frameCount,char *frameFormat,const char *plistFile,char *animationName)
{
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(plistFile);
	CCArray *animFrames = CCArray::create();
	char str[64] = {0};
	
	for (int i = startFrameIndex; i < startFrameIndex+frameCount; i++)
	{
		sprintf(str, frameFormat, i);
		CCLOG("%s",str);
		CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);
		animFrames->addObject(frame);
	}
	CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.15f);	
	CCAnimationCache::sharedAnimationCache()->addAnimation(animation, animationName);
}

 bool SpriteHelper::collide(CCSprite *a, CCSprite *b)
 {
	 if(!a || !b)
	 {
		 return false;
	 }
	 CCRect aRect = spriteRect(a);
	 CCRect bRect = spriteRect(b);
	 if (aRect.intersectsRect(bRect)) {
		 return true;
	 }
	 return false;
 }

 CCRect SpriteHelper::spriteRect(CCSprite * sprite)
 {	
	 CCPoint pos = sprite->getPosition();	 
	 return CCRectMake(pos.x -sprite->getContentSize().width/2, 
		 pos.y-sprite->getContentSize().height/2, 
		 sprite->getContentSize().width, 
		 pos.y-sprite->getContentSize().height);
 }

 void SpriteHelper::InitFrameSet( int startFrameIndex, int frameCount, char *frameFormat, const char *plistFile,SpriteAction action,SpriteTag type)
{
	char str[64] = {0};
	sprintf(str,"sprite_ani_action_%d_%d",(int)type,(int)action);
	SpriteHelper * helper=new SpriteHelper();
	helper->initSpriteActionAnimation(startFrameIndex,
		frameCount,
		frameFormat,
		plistFile,
		str);
		
}

 CCAction * SpriteHelper::SetCurrentAction(CCSprite *sprite,SpriteAction action,SpriteTag type)
{
	char str[64] = {0};
	CCAction * currentAction=NULL;
	sprintf(str,"sprite_ani_action_%d_%d",(int)type,(int)action);
	CCAnimation * actonAni=CCAnimationCache::sharedAnimationCache()->animationByName(str);
	CCAssert(actonAni,"no this action!");
	CCAnimate *animate = CCAnimate::create(actonAni);
	sprite->stopAllActions();
	currentAction=CCRepeatForever::create(animate);
	sprite->runAction(currentAction);
	return currentAction;
}

 CCAction * SpriteHelper::SetCurrentActionNoRepeat(CCSprite *sprite,SpriteAction action,SpriteTag type,CCCallFuncN * callback)
 {
	 char str[64] = {0};
	 CCAction * currentAction=NULL;
	 sprintf(str,"sprite_ani_action_%d_%d",(int)type,(int)action);
	 CCAnimation * actonAni=CCAnimationCache::sharedAnimationCache()->animationByName(str);
	 CCAnimate *animate = CCAnimate::create(actonAni);
	 sprite->stopAllActions();
	 currentAction=CCAnimate::create(actonAni);
	 if(callback==NULL)
	 {
		 sprite->runAction(animate);
	 }
	 else
		 sprite->runAction(CCSequence::create(animate,callback,NULL));
	 return currentAction;
 }